package com.zlp.mall.modules.ums.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.ums.model.UmsMenu;
import com.zlp.mall.modules.ums.model.UmsResource;
import com.zlp.mall.modules.ums.model.UmsRole;
import com.zlp.mall.modules.ums.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@Api(value = "RoleController", tags = "后台用户角色管理")
public class RoleController {

    private final UmsRoleService roleService;

    @PostMapping(value = "/create")
    @ApiOperation(value = "添加角色")
    public R create(@RequestBody UmsRole role) {

        boolean success = roleService.create(role);
        return R.success(success);
    }

    @PostMapping(value = "/update/{id}")
    @ApiOperation(value = "修改角色",tags = "修改角色")
    public R update(@PathVariable Long id, @RequestBody UmsRole role) {

        role.setId(id);
        boolean success = roleService.updateById(role);
        return R.success(success);
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "批量删除角色",tags = "批量删除角色")
    public R delete(@RequestParam("ids") List<Long> ids) {

        boolean success = roleService.delete(ids);
        return R.success(success);
    }


    @GetMapping(value = "/listAll")
    @ApiOperation(value = "获取所有角色")
    public R<List<UmsRole>> listAll() {

        return R.success(roleService.list());
    }

    @GetMapping(value = "/pageList")
    @ApiOperation(value = "分页获取角色列表",tags = "分页获取角色列表")
    public R<Pager<UmsRole>> pageList(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {

        Page<UmsRole> roleList = roleService.list(keyword, pageSize, pageNum);
        return R.success(Pager.restPage(roleList));
    }

    @PostMapping(value = "/updateStatus/{id}")
    @ApiOperation(value = "修改角色状态",tags = "修改角色状态")
    public R updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {

        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        umsRole.setStatus(status);
        boolean success = roleService.updateById(umsRole);
        return R.success(success);
    }

    @GetMapping(value = "/listMenu/{roleId}")
    @ApiOperation(value = "获取角色相关菜单",tags = "获取角色相关菜单")
    public R<List<UmsMenu>> listMenu(@PathVariable Long roleId) {

        List<UmsMenu> roleList = roleService.listMenu(roleId);
        return R.success(roleList);
    }

    @GetMapping(value = "/listResource/{roleId}")
    @ApiOperation(value = "获取角色相关资源",tags = "获取角色相关资源")
    public R<List<UmsResource>> listResource(@PathVariable Long roleId) {

        List<UmsResource> roleList = roleService.listResource(roleId);
        return R.success(roleList);
    }

    @PostMapping(value = "/allocMenu")
    @ApiOperation(value = "给角色分配菜单",tags = "给角色分配菜单")
    public R allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {

        int count = roleService.allocMenu(roleId, menuIds);
        return R.success(count);
    }

    @PostMapping(value = "/allocResource")
    @ApiOperation(value = "给角色分配资源",tags = "给角色分配资源")
    public R allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {

        int count = roleService.allocResource(roleId, resourceIds);
        return R.success(count);
    }

}
